package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ResponseEntity.ResponseEntity;
import com.woniuxy.dao.RbacRoleDao;
import com.woniuxy.entity.RbacRole;
import com.woniuxy.entity.RolerMenuDto;
import com.woniuxy.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RbacRoleServiceImpl implements RbacRoleService {
    @Autowired(required = false)
    RbacRoleDao rbacRoleDao;

    @Override
    public ResponseEntity findAllRole(int page, int size, RbacRole rbacRole) {
        PageHelper.startPage(page, size);

        List<RbacRole> allRole = rbacRoleDao.findAllRole(rbacRole);
        //现在使用PageInfo.of(rbac);是最新版本写的方式，可以直接获得各种分页信息和数据
        //将数据和分页信息一起封装了
        PageInfo<RbacRole> pageInfo =PageInfo.of(allRole);

        //创建返回类型对象
        ResponseEntity<PageInfo> response = new ResponseEntity<>();

        if (allRole.size() > 0){
            response.setData(pageInfo);
            response.setCode(200);
            response.setMessage("查询成功");
        }else {
            response.setData(null);
            response.setCode(500);
            response.setMessage("查询失败");
        }

        return response;
    }

    @Override
    public ResponseEntity findByid(int roleid) {
        List<Integer> list=rbacRoleDao.findByid(roleid);
        return new ResponseEntity(200,"查询成功",list);
    }

    @Override
    public ResponseEntity updateRoleMenu(RolerMenuDto rolerMenuDto) {
        int id= rbacRoleDao.deleteRoleMenu(rolerMenuDto.getRoleId());

        //有可能前端传递过来的数据中菜单id的数量为0，说明就是全部删除，就不要进入这个插入方法
        if (rolerMenuDto.getModuleIds().size()!=0){
            rbacRoleDao.insertRoleMenu(rolerMenuDto);
        }

        return null;
    }
}
